<template>
  <div class="data-query">
    <p class="data-total">资料库文件数：{{ fileTotal }}份</p>
    <div class="search-box">
      <div class="search-content">
        <el-input
          v-model="content"
          placeholder="请输入关键词"
          class="content-input"
          @keyup.enter.native="onSubmit"
        ></el-input>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </div>
      <p class="search-time" v-if="isShow">找到{{ total }}份相关资料</p>
    </div>
    <div class="result-list">
      <div
        v-for="item in list"
        :key="'result' + item.id"
        class="result-list-item"
      >
        <div class="articleBox" @click="viewRefer(item)">
          <div class="res-header">
            <img :src="item.icon" class="icon" />
            <h2>{{ item.name }}</h2>
          </div>
          <div class="res-main" v-html="item.content"></div>
        </div>
        <div class="result-indu">
          <div class="indu-box" v-if="item.publishDate">
            <span>发文日期：</span>
            <span>{{ item.publishDate }}</span>
          </div>
          <div class="indu-box" v-if="item.receivedDate">
            <span>收文日期：</span>
            <span>{{ item.receivedDate }}</span>
          </div>
          <div class="indu-box" v-if="item.issuingEnterprise">
            <span>来文单位：</span>
            <span>{{ item.issuingEnterprise }}</span>
          </div>
          <div class="indu-box" v-if="item.documentNumber">
            <span>来文文号：</span>
            <span>{{ item.documentNumber }}</span>
          </div>
          <!--<div class="indu-box">
            <span>上传者：</span>
            <span>{{ item.createBy }}</span>
          </div>
          -->
          <div class="indu-box">
            <span>下载次数：</span>
            <span>{{ item.amountOfDownloads }}</span>
          </div>
          <div class="indu-box" v-if="item.location">
            <span>档案位置：</span>
            <span>{{ item.location }}</span>
          </div>
          <div class="indu-box">
            <span class="download" @click="downloadDetail(item)">下载</span>
          </div>
        </div>
      </div>
    </div>
    <el-row class="pages" :gutter="10">
      <el-col :span="24">
        <div style="text-algin: center">
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="pageNum"
            :limit.sync="pageSize"
            :pageSizes="pageSizes"
            @pagination="getArchivesSearch"
          />
          <!-- </el-pagination> -->
        </div>
      </el-col>
    </el-row>
    <div class="files-add-window" v-if="isRefer">
      <div class="border">
        <el-button type="info" size="small" @click="gobackRefer"
          >返回</el-button
        >
      </div>
      <div class="files-add-basic">
        <div class="scan-file-list">
          <div
            class="image-box"
            v-for="(item, index) in showImageList"
            :key="item.filePath"
          >
            <div class="ImgItem">
              <!-- <div class="pic-op-btn">
                <i
                  class="el-icon-download pic-download-icon"
                  @click="downloadImg(item, index)"
                  title="下载"
                ></i>
              </div> -->
              <el-image
                style="width: 100%; height: 120px"
                :src="item.fileShowPath"
                :preview-src-list="srcList"
                fit="contain"
              >
              </el-image>
            </div>
            <p class="scan-filename">{{ item.fileName }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  archivesSearch,
  getFileTotal,
  kkfileUrl,
  arAnnexList,
} from "@/api/files/basics";
import { Base64 } from "js-base64";
import { encode, decode } from "js-base64";
export default {
  name: "DataQuery",
  components: {},
  data() {
    return {
      content: "",
      list: [],
      total: 0,
      pageSize: 10,
      pageNum: 1,
      pageSizes: [10, 20, 30, 40, 50],
      fileTotal: 0,
      isRefer: false,
      kkFileUrl: "",
      referObj: {
        id: "",
      },
      imageList: [],
      showImageList: [],
      srcList: [],
      isShow: false,
    };
  },
  methods: {
    _getFileTotal() {
      getFileTotal().then((res) => {
        this.fileTotal = res.data;
      });
    },
    getArchivesSearch() {
      let data = {
        content: this.content,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      archivesSearch(data).then((res) => {
        if (res.code == 200) {
          this.list = res.rows;
          this.total = res.total;
          if (!this.content) {
            this.isShow = false;
          } else {
            this.isShow = true;
          }
        }
      });
    },
    onSubmit() {
      this.pageNum = 1;
      this.getArchivesSearch();
    },
    downloadDetail(row) {
      let fileName = row.fileName;
      let data = {
        id: row.id,
      };
      this.downloadGet("/office/archives/download", data, fileName);
    },
    getkkfileUrl() {
      kkfileUrl().then((res) => {
        if (res.code == 200) {
          this.kkFileUrl = res.data;
        } else {
          this.kkFileUrl = "";
        }
      });
    },
    viewRefer(row) {
      if (row.type == 0) {
        if (this.kkFileUrl) {
          var previewUrl = row.filePath; //要预览文件的访问地址
          window.open(
            this.kkFileUrl +
              "?url=" +
              encodeURIComponent(Base64.encode(previewUrl))
          );
        }
      } else {
        this.referObj = {
          id: row.id,
        };
        this.isRefer = true;
        this.getAnnexList();
      }
    },
    //获取个人附件信息
    getAnnexList() {
      this.loading2 = true;
      let pid = this.referObj.id;
      arAnnexList(pid).then((res) => {
        if (res.code == 200) {
          this.loading2 = false;
          let imageList = [];
          res.data.forEach((item) => {
            let obj = {
              id: item.id,
              fileShowPath: item.fileShowPath, //预览地址
              filePath: item.filePath, //下载地址
              fileName: item.fileName,
              typeName: item.typeName,
            };
            imageList.push(obj);
          });
          this.imageList = imageList;
          this.showImageList = JSON.parse(JSON.stringify(this.imageList));
          this.getSrcList();
        } else {
          this.loading2 = false;
          this.$message.warning(res.msg);
        }
      });
    },
    getSrcList() {
      let srcList = [];
      this.showImageList.forEach((item) => {
        srcList.push(item.fileShowPath);
      });
      this.srcList = srcList;
    },
    gobackRefer() {
      this.isRefer = false;
      this.imageList = [];
      this.showImageList = [];
      this.srcList = [];
    },
  },
  mounted() {
    this.getkkfileUrl();
    this._getFileTotal();
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped >
.files-add-window {
  position: absolute;
  right: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #fff;
  z-index: 99;
  overflow-y: auto;
  .border {
    padding: 0 10px 10px 10px;
    border-bottom: 1px #bbb solid;
  }
  .scan-file-list {
    clear: both;
    overflow: hidden;
    padding: 20px;
    .image-box {
      float: left;
      margin: 4px;
    }
    .ImgItem {
      width: 240px;
      height: 122px;
      border: 1px #bbb solid;
    }
  }
}
.scan-filename {
  font-size: 14px;
  width: 100%;
  color: #000;
  text-align: center;
}
.data-query {
  padding: 20px 40px;
  .data-total {
    font-size: 16px;
    margin-bottom: 60px;
  }
  .search-box {
    width: 680px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin: 0 auto 30px;

    .search-content {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 8px;
      .content-input {
        width: 600px;
        margin-right: 10px;
      }
    }
    .search-time {
      font-size: 12px;
      color: blue;
    }
  }
  .result-list {
    margin-bottom: 30px;
    &-item {
      margin-bottom: 24px;
      .articleBox {
        cursor: pointer;
        margin-bottom: 10px;
      }
      .res-header {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        margin-bottom: 10px;
        .icon {
          width: 32px;
          height: 32px;
          margin-right: 10px;
        }
        > h2 {
          font-size: 18px;
          color: #000;
          margin: 0;
          padding: 0;
        }
      }
      .res-main {
        width: 100%;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3; /* 定义显示的行数 */
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 16px;
        color: #000;
        line-height: 28px;
      }
      .result-indu {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        .indu-box {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-right: 12px;
          font-size: 14px;
          > span:nth-child(1) {
            color: #4ea1db;
            font-size: 12px;
          }
          > span.download {
            color: crimson;
            cursor: pointer;
          }
        }
      }
    }
  }
}
p {
  margin: 0;
  padding: 0;
}
</style>
<style rel="stylesheet/scss" lang="scss">
.search-content {
  .el-input--medium .el-input__inner {
    height: 40px;
    line-height: 40px;
  }
  .el-button--medium {
    padding: 13px 20px;
  }
}
.res-main {
  em {
    color: red;
  }
}
</style>